package com.pzhu.dormitory.controller;


import com.pzhu.dormitory.entity.MenuRoleRelationshipEntity;
import com.pzhu.dormitory.service.MenuRoleRelationshipService;
import com.pzhu.dormitory.utils.PageUtils;
import com.pzhu.dormitory.utils.R;
import com.pzhu.dormitory.utils.Rb;
import com.pzhu.dormitory.vo.MenuRolesGrantVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;


/**
 * 菜单角色关系
 *
 * @author
 * @email
 * @date 2021-04-17 23:59:24
 */
@Slf4j
@RestController
@Api(value = "MenuRoleRelationshipController", tags = "菜单角色关系")
@RequestMapping("elective/menurolerelationship")
public class MenuRoleRelationshipController {
    @Autowired
    private MenuRoleRelationshipService menuRoleRelationshipService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "获取菜单角色关系list")
    public R<MenuRoleRelationshipEntity> list(@RequestParam Map<String, Object> params) {
        PageUtils<MenuRoleRelationshipEntity> page = menuRoleRelationshipService.queryPage(params);
        return Rb.a(MenuRoleRelationshipEntity.class).success().page(page).build();
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation(value = "获取一个菜单角色关系信息")
    public R<MenuRoleRelationshipEntity> info(@Validated @PathVariable("id") Integer id) {
        MenuRoleRelationshipEntity menuRoleRelationship = menuRoleRelationshipService.getById(id);
        return Rb.a(MenuRoleRelationshipEntity.class).success().withData(menuRoleRelationship).build();
    }

    /**
     * 保存
     */
    @ApiOperation(value = "保存菜单角色关系信息")
    @PostMapping("/save")
    public R<MenuRoleRelationshipEntity> save(@Validated @RequestBody MenuRoleRelationshipEntity menuRoleRelationship) {
        menuRoleRelationshipService.save(menuRoleRelationship);
        return Rb.a(MenuRoleRelationshipEntity.class).success().build();
    }

    /**
     * 修改
     */
    @ApiOperation(value = "更新菜单角色关系信息")
    @PutMapping("/update")
    public R<MenuRoleRelationshipEntity> update(@RequestBody MenuRoleRelationshipEntity menuRoleRelationship) {
        menuRoleRelationshipService.updateById(menuRoleRelationship);
        return Rb.a(MenuRoleRelationshipEntity.class).success().build();
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除菜单角色关系信息")
    @DeleteMapping("/delete")
    public R<MenuRoleRelationshipEntity> delete(@RequestBody Integer[] ids) {
        menuRoleRelationshipService.removeByIds(Arrays.asList(ids));
        return Rb.a(MenuRoleRelationshipEntity.class).success().build();
    }

    /**
     * 分配角色
     */
    @PutMapping("/grant")
    public R<MenuRoleRelationshipEntity> grantRole(@RequestBody MenuRolesGrantVo menuRolesGrantVo) {
        menuRoleRelationshipService.saveGrantRole(menuRolesGrantVo);
        return Rb.a(MenuRoleRelationshipEntity.class).success().build();
    }

}
